

package com.bei_en.teacher.admin.controller.forecast;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.forecast.bean.Forecast;
import com.bei_en.teacher.admin.model.forecast.condition.ForecastCondition;
import com.bei_en.teacher.admin.model.forecast.vo.ForecastVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.service.forecast.ForecastService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-10-8 21:41:37
 */
@Controller
@RequestMapping("/api/Forecast")
public class ForecastAPPController extends BaseController {

    @Autowired
    ForecastService forecastService;

    
    
    @RequestMapping("/forecast")
	@ResponseBody
    public JsonResult forecast(Model model,@Valid  ForecastCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<ForecastVo> list =new ArrayList<ForecastVo>();
		   list = forecastService.findForecastPage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   ForecastVo forecastVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   forecastVo=list.get(i);
	   seetingJsonObject(object,"id",  forecastVo.getId());
	   seetingJsonObject(object,"userId",  forecastVo.getUserId());
	   seetingJsonObject(object,"fraction",  forecastVo.getFraction());
	   seetingJsonObject(object,"typeId",  forecastVo.getTypeId());
	   seetingJsonObject(object,"createTime",  forecastVo.getCreateTime());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/forecastAdd")
    @ResponseBody
    public JsonResult forecastAdd(Model model,@Valid  ForecastCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
      		UsersVo us=	getUser(session);
      		if(us!=null){
      			 ForecastVo forecast=new ForecastVo();
      		   forecast.setUserId(us.getUserId());
      		   forecast.setFraction(vo.getFraction());
      		   forecast.setTypeId(vo.getTypeId());
      		   forecast.setCreateTime(new Date());
      			forecastService.insert(forecast);
      		}
			JSONObject jsonObject = new JSONObject();
			//预测分数
			ForecastVo forecastCondition=new ForecastVo();
			forecastCondition.setTypeId(vo.getTypeId());
			 List<Integer> inte=	forecastService.findAllIds(forecastCondition);
			 if(inte.size()>0){
				 int index=(int) Math.floor(inte.size()/10);
				 if(index<=0){
					 index=1;
				 }
				 ForecastVo forecastVo=  forecastService.findById(inte.get(index));
	
				 if(forecastVo!=null){
					 if(forecastVo.getFraction()<80){
						 jsonObject.put("target", 80+"");
					 }else{
						 jsonObject.put("target", forecastVo.getFraction());
					 }
				
				 }else{
					 jsonObject.put("target",80+"");
				 }
			 }else{
				 jsonObject.put("target",80+"");
			 }
			return new JsonSuccessResult(session.getId(), jsonObject);
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
    @RequestMapping("/forecastEdit")
    public JsonResult forecastEdit(Model model,@Valid  ForecastCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	ForecastVo forecast=new ForecastVo();
	
		forecastService.updateSelective(forecast);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
